package com.apes.framework.plugin.esb.parser.element;

import com.apes.framework.plugin.esb.processor.ChainProcessor;
import com.apes.framework.plugin.esb.processor.router.RouterProcessor;
import com.apes.framework.plugin.filereader.xml.ApesElement;
import com.apes.framework.util.LogicalUtil;
import org.springframework.stereotype.Component;


/**
 * 功能：router元素解析器
 *
 * @author xul
 * @create 2018-04-16 14:25
 */
@Component
public class RouterElementParser extends AbstractNestedProcessorParser {

    /**
     * 可解析的元素列表
     **/
    private final String[] elementNames = {"router"};

    /**
     * 功能：可解析的元素列表
     */
    @Override
    public String[] getNames() {
        return elementNames;
    }

    @Override
    protected void parseNestedElements(ApesElement element, ChainProcessor processor) {
        element.getNestedElements().stream().forEach(nestedElement -> {
            if (nestedElement.getName().equals("if")) {
                ((RouterProcessor) processor).addFilter(nestedElement.parse());
            } else if (LogicalUtil.in(nestedElement.getName(), "other", "elseif")) {
                ((RouterProcessor) processor).setOther(nestedElement.parse());
            }
        });
    }

    @Override
    <T extends Class> T getChainProcessorClass() {
        return (T) RouterProcessor.class;
    }

}
